package com.mimi.practice.ui

import android.content.Intent
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.mimi.practice.PermissionControl
import com.mimi.practice.base.BaseActivity
import com.mimi.practice.databinding.ActivityIndexBinding
import com.mimi.practice.ui.dialog.LoadingView
import com.mimi.practice.viewmodel.IndexViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class IndexActivity : BaseActivity() {

    private lateinit var mBinding: ActivityIndexBinding
    private val indexViewModel: IndexViewModel by lazy { ViewModelProvider(this)[IndexViewModel::class.java] }

    private lateinit var settingsLauncher: ActivityResultLauncher<Intent>
    private lateinit var bluetoothEnableLauncher: ActivityResultLauncher<Intent>
    private lateinit var permissionControl: PermissionControl
    private lateinit var loadingPopup: LoadingView

    override fun getLayoutView(): View {
        mBinding = ActivityIndexBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {

    }

    override fun initView() {
        initLaunchers()
        mBinding.toolbar.title.text = "运动建康"
        mBinding.toolbar.back.visibility = View.GONE
        mBinding.toolbar.more.setOnClickListener {
            XPopup.Builder(this).atView(mBinding.toolbar.more).hasShadowBg(false)
                .isLightStatusBar(true).isLightNavigationBar(true)
                .asAttachList(arrayOf("扫描所有设备"), intArrayOf()) { position: Int, _: String? ->
                    if (position == 0) {
                        permissionControl.checkBluePermission {
                            if (!it) {
                                ToastUtils.showShort("请检查蓝牙开关及相关权限")
                            } else {
                                startScan()
                            }
                        }
                    }
                }.show()
        }
    }

    private fun startScan() {
        loadingPopup = XPopup.Builder(this).dismissOnTouchOutside(false).dismissOnBackPressed(false)
            .isLightNavigationBar(true).asCustom(LoadingView(this, "正在扫描设备..."))
            .show() as LoadingView
        indexViewModel.startScan()
    }

    private fun initLaunchers() {

        // 初始化 ActivityResultLauncher
        settingsLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { // 处理设置页面返回后的逻辑
                permissionControl.checkBluePermission {}
            }
        bluetoothEnableLauncher =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                if (result.resultCode == RESULT_OK) { // 蓝牙已启用，继续执行后续操作
                    LogUtils.dTag("Bluetooth", "Bluetooth enabled")
                } else { // 用户拒绝启用蓝牙，处理相应逻辑
                    LogUtils.dTag("Bluetooth", "Bluetooth not enabled")
                }
            }

        // 以任意顺序传递这些 launcher
        val launchers = mapOf(
            "bluetooth" to bluetoothEnableLauncher,
            "settings" to settingsLauncher,
        )

        // 初始化 OfflinePermissions 并传递 ActivityResultLauncher
        permissionControl = PermissionControl(this, launchers)
    }
}